import com.mysql.cj.protocol.Resultset;

import java.sql.*;
import java.util.Scanner;

public class Main {
    //99乘法表


    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement preparedStatement =null;
        ResultSet resultset =null;



        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //创建连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "1234");
            if(connection!=null){
                System.out.println("连接成功");
            }

            //预处理
            String sql = "select * from user ";
             preparedStatement = connection.prepareStatement(sql);


            //执行
            resultset = preparedStatement.executeQuery();

            //展示结果
            while(resultset.next()){
                int id=resultset.getInt(1);
                String name =resultset.getString(2);
                String pass =resultset.getString(3);
                System.out.println(id+"_______"+name+"______"+pass);
            }





        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (resultset!=null)
                resultset.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if(preparedStatement!=null)
                preparedStatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if(connection!=null)
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
